package com.thoughtworks.jsstaticanalysis;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JavascriptFileSummary {

    private List<FoundFunction> functions = new ArrayList<FoundFunction>();
    private List<RecognitionException> parserErrors = new ArrayList<RecognitionException>();
    private File jsFile;

    public JavascriptFileSummary(File jsFile) {
        try {
            parserJSFile(jsFile);
        } catch (IOException ignore) {
        } catch (RecognitionException e) {
            parserErrors.add(e);
        }
    }

    private void parserJSFile(File jsFile) throws IOException, RecognitionException {
        this.jsFile = jsFile;
        ANTLRInputStream stream = new ANTLRInputStream(new FileInputStream(jsFile));
        JavascriptLexer lexer = new JavascriptLexer(stream);
        CommonTokenStream commonToken = new CommonTokenStream(lexer);
        JavascriptParser parser = new JavascriptParser(commonToken);
        parser.fileName = jsFile.getName();

        parser.program();
        functions = parser.getFunctions();
    }

    public List<FoundFunction> getFunctions() {
        return functions;
    }

    public String getFolderName() {
        return jsFile.getParent();
    }

    public String getFileName() {
        return jsFile.getName();
    }

    public File getJsFile() {
        return jsFile;
    }
}
